﻿function getBalanceCardLotList() {
    var html = "<div>&nbsp;<img src=\"/Content/images/loading.gif\" /> 正在请求数据,请稍候......</div>";
    $("#divRecharge").html(html);

    $("#idBalanceCardLotList").attr("class", "on");
    $("#idBalanceHistory").removeAttr("class");

    $.ajax({
        type: "GET",
        url: "/UserHome/BalanceCardLotList",
        cache: false,
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data == "") {
                    $("#divRecharge").html("暂无可购买充值卡");
                } else {
                    $("#divRecharge").html(data);
                }
            }
            lazyLoad.Run();
        }
    });
}

function getBalanceHistory() {
    var html = "<div>&nbsp;<img src=\"/Content/images/loading.gif\" /> 正在请求数据,请稍候......</div>";
    $("#divRecharge").html(html);

    $("#idBalanceHistory").attr("class", "on"); idBalanceCardLotList
    $("#idBalanceCardLotList").removeAttr("class");

    $.ajax({
        type: "GET",
        url: "/UserHome/BalanceHistory",
        cache: false,
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data == "") {
                    $("#divRecharge").html("暂无充值记录");
                } else {
                    $("#divRecharge").html(data);
                }
            }
            lazyLoad.Run();
        }
    });
}

function getPagedBalanceHistory(pageIndex) {
    pageIndex = pageIndex + 1;
    $.ajax({
        type: "GET",
        url: "/UserHome/BalanceHistory?pageIndex=" + pageIndex,
        cache: false,
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data == undefined || data == "") {
                    alert("该页无数据");
                } else {
                    $("#divRecharge").html(data);
                }
            }
        }
    });
}

function showSafetyTips() {
    ShowBack();
    $(".my_pay_pop").show();
}

function closeSafetyTips() {
    $(".my_pay_pop").hide();
    HideBack();
}

function goSetPayPassword() {
    window.location.href = "/UserHome/PayPasswordForPhone";
}

function selectCardLot(lotSysNo) {
    $("#radio" + lotSysNo).attr("checked", "checked");
    $("#link" + lotSysNo).attr("class", "on");
    $("#radio" + lotSysNo).parent().siblings("li").find("input[type=radio]").attr("checked", false);
    $("#radio" + lotSysNo).parent().siblings("li").find("a").removeAttr("class");
}

function RechargeNow(domain) {
    var lotSysNo = $(".my_pay_sku").find("a[class=on]").first().attr("CardLotSysNo");
    if (lotSysNo == undefined || lotSysNo == "")
    {
        alert("请选择充值面额");
        return;
    }

    var qty = "1";
    var cardLot = lotSysNo + "," + qty;
    //创建订单
    $.ajax({
        type: "POST",
        url: "/GiftCard/CreateCardOrder?CardLot=" + cardLot,
        cache: false,
        beforeSend: function () {
            Loadding("show");
        },
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data.status == -1) {
                    alert("数据加载失败");
                    Loadding("hide");
                    return;
                } else if (data.error != "0") {
                    if (data.error == "100") {
                        window.location.href = data.data;
                    } else {
                        alert("数据加载失败: " + data.message);
                        Loadding("hide");
                        return;
                    }
                } else {
                    //提交订单
                    $.ajax({
                        type: "POST",
                        url: "/GiftCard/SubmitCardOrder",
                        cache: false,
                        success: function (data) {
                            if (AjaxValidate(data)) {
                                if (data.status == -1) {
                                    alert("数据加载失败");
                                    Loadding("hide");
                                    return;
                                } else if (data.error != "0") {
                                    alert("数据加载失败: " + data.message);
                                    Loadding("hide");
                                    return;
                                } else {
                                    var id = data.data.GiftCardOrderSysNo;
                                    var sono = data.data.GiftCardOrderID;
                                    var url = domain + "/Shopping/OnlinePay?id=" + id + "&sono=" + sono + "&osid=0&type=gc";
                                    window.location.href = url;
                                }
                            }
                        }
                    });
                }
            }
        }
    });
}

function payPassword(type,index) {
    var html = "<div>&nbsp;<img src=\"/Content/images/loading.gif\" /> 正在请求数据,请稍候......</div>";
    if (type == 1)
    $(".my_pay_code").html(html);
    else
        $(".my_pay_site").html(html);
    $("#payPassword" + index).attr("class", "on");
    $("#payPassword" + index).siblings("a").removeAttr("class");

    $.ajax({
        type: "GET",
        url: "/UserHome/PayPasswordPartialPage?type="+type+"&index=" + index,
        cache: false,
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data == "") {
                    if(type==1)
                        $(".my_pay_code").html("暂无数据");
                    else 
                        $(".my_pay_site").html("暂无数据");
                } else {
                    if (type == 1)
                        $(".my_pay_code").html(data);
                    else
                        $(".my_pay_site").html(data);
                }
            }
            lazyLoad.Run();
        }
    });
}

function setPayPassword() {
    var newPassword = $("#newPassword").val();
    var confirmNewPassword = $("#confirmNewPassword").val();
    var checkCode=$("#checkCode").val();
    if (!checkPayPassword(newPassword, confirmNewPassword)) {
        return;
    }

    $.ajax({
        type: "POST",
        url: "/UserHome/SetPayPassword",
        data: {authCode:checkCode, pwd: newPassword, confirmPwd: confirmNewPassword },
        cache: false,
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data.error == "0") {
                    alert("您已成功设置支付密码.",1);
                    $("#newPassword").val("");
                    $("#confirmNewPassword").val("");
                } else if (data.error == 1) {
                    window.location.href = "/login.html";
                } else {
                    alert("支付密码设置失败,错误信息：" + data.message);
                    $("#newPassword").val("");
                    $("#confirmNewPassword").val("");
                }
            } else {
                alert(data);
            }
            lazyLoad.Run();
        }
    });
}

function setPayPasswordByEmail() {
    var newPassword = $("#newPassword").val();
    var confirmNewPassword = $("#confirmNewPassword").val();
    if (!checkPayPassword(newPassword, confirmNewPassword)) {
        return;
    }

    $.ajax({
        type: "POST",
        url: "/UserHome/SetPayPasswordByEmail",
        data: {pwd: newPassword, confirmPwd: confirmNewPassword },
        cache: false,
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data.error == "0") {
                    alert("您已成功设置支付密码.", 1);
                    $("#newPassword").val("");
                    $("#confirmNewPassword").val("");
                } else if (data.error == 1) {
                    window.location.href = "/login.html";
                } else {
                    alert("支付密码设置失败,错误信息：" + data.message);
                    $("#newPassword").val("");
                    $("#confirmNewPassword").val("");
                }
            } else {
                alert(data);
            }
            lazyLoad.Run();
        }
    });
}

function checkPayPassword(newPassword, confirmNewPassword, oldPassword, authCode) {
    if (newPassword == "" || confirmNewPassword == "") {
        alert("支付密码设置失败,错误信息：请输入6位数字密码");
        return false;
    }
    if (newPassword != confirmNewPassword) {
        alert("支付密码设置失败,错误信息：两次输入密码不一致");
        return false;
    }
    var reg = new RegExp("^[0-9]{6}$");
    if (!reg.test(newPassword) || !reg.test(confirmNewPassword)) {
        alert("支付密码设置失败,错误信息：密码只能为6位数字");
        return false;
    }

    if (oldPassword != undefined) {
        if (!reg.test(oldPassword)) {
            alert("支付密码修改失败,错误信息：请输入6位数字旧密码");
            return false;
        }
    }

    if (authCode != undefined) {
        if (authCode == "") {
            alert("支付密码设置失败,错误信息：请输入验证码");
            return false;
        }
    }

    return true;
}

function modifyPayPassword() {
    var oldPassword = $("#oldPassword").val();
    var newPassword = $("#newPassword").val();
    var confirmNewPassword = $("#confirmNewPassword").val();

    if (!checkPayPassword(newPassword, confirmNewPassword, oldPassword)) {
        return;
    }

    $.ajax({
        type: "POST",
        url: "/UserHome/ChangePayPassword",
        data: { oldPwd:oldPassword, pwd: newPassword, confirmPwd: confirmNewPassword },
        cache: false,
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data.error == "0") {
                    alert("您已成功修改支付密码.",1);
                    $("#oldPassword").val("");
                    $("#newPassword").val("");
                    $("#confirmNewPassword").val("");
                } else if (data.error == 1) {
                    window.location.href = "/login.html";
                } else {
                    alert("支付密码设置失败,错误信息：" + data.message);
                    $("#oldPassword").val("");
                    $("#newPassword").val("");
                    $("#confirmNewPassword").val("");
                }
            }
            lazyLoad.Run();
        }
    });
}

function sendResetPayPasswordSms() {
    $.ajax({
        type: "POST",
        url: "/UserHome/SendSmsResetPayPass",
        data: { },
        cache: false,
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data.error == "0") {
                    //发送成功
                    $(".btn01").attr("disabled", "disabled");
                    $(".btn02").show();

                    var i = 60;
                    var timer = setInterval(function () {
                        if (i == -1) {
                            clearInterval(timer);
                            $(".btn01").removeAttr("disabled");
                            $(".btn02").hide();
                        } else {
                            $("#countDown").html(i+"秒");
                            --i;
                        }
                    }, 1000);
                } else if (data.error == 1) {
                    window.location.href = "/login.html";
                } else {
                    alert("错误信息：" + data.message);
                }
            }
            lazyLoad.Run();
        }
    });
}

function sendResetPayPasswordEmail() {
    $.ajax({
        type: "POST",
        url: "/UserHome/SendEmailResetPayPass",
        data: {},
        cache: false,
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data.error == "0") {
                    //发送成功
                    $(".btn03").attr("disabled", "disabled");
                    $(".prompt").show();

                } else if (data.error == 1) {
                    window.location.href = "/login.html";
                } else {
                    alert("错误信息：" + data.message);
                }
            }
            lazyLoad.Run();
        }
    });
}

function sendSetPayPasswordSms() {
    $.ajax({
        type: "POST",
        url: "/UserHome/SendSmsSetPayPass",
        data: {},
        cache: false,
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data.error == "0") {
                    //发送成功
                    $(".btn01").attr("disabled", "disabled");
                    $(".btn02").show();

                    var i = 60;
                    var timer = setInterval(function () {
                        if (i == -1) {
                            clearInterval(timer);
                            $(".btn01").removeAttr("disabled");
                            $(".btn02").hide();
                        } else {
                            $("#countDown").html(i + "秒");
                            --i;
                        }
                    }, 1000);
                } else if (data.error == 1) {
                    window.location.href = "/login.html";
                } else {
                    alert("错误信息：" + data.message);
                }
            }
            lazyLoad.Run();
        }
    });
}

function sendSetPayPasswordEmail() {
    $.ajax({
        type: "POST",
        url: "/UserHome/SendEmailSetPayPass",
        data: {},
        cache: false,
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data.error == "0") {
                 // 发送成功
                    $(".btn01").attr("disabled", "disabled");
                    $(".prompt").show();
                } else if (data.error == 1) {
                    window.location.href = "/login.html";
                } else if (data.error == "-3") {
                    alert(data.message);
                } else
                    alert("错误信息：" + data.message);
            }
            lazyLoad.Run();
        }
    });
}

function resetPayPassword() {
    var checkCode = $("#checkCode").val();
    var newPassword = $("#newPassword").val();
    var confirmNewPassword = $("#confirmNewPassword").val();

    if (!checkPayPassword(newPassword, confirmNewPassword, undefined, checkCode)) {
        return;
    }

    $.ajax({
        type: "POST",
        url: "/UserHome/ResetPayPwd",
        data: { authCode: checkCode, pwd: newPassword, confirmPwd: confirmNewPassword },
        cache: false,
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data.error == "0") {
                    alert("您已成功修改支付密码.",1);
                    $("#checkCode").val("");
                    $("#newPassword").val("");
                    $("#confirmNewPassword").val("");
                } else if (data.error == 1) {
                    window.location.href = "/login.html";
                } else {
                    alert("错误信息：" + data.message);
                    $("#checkCode").val("");
                    $("#newPassword").val("");
                    $("#confirmNewPassword").val("");
                }
            }
            lazyLoad.Run();
        }
    });
}

function resetPayPasswordbyEmail() {
    var newPassword = $("#newPassword").val();
    var confirmNewPassword = $("#confirmNewPassword").val();

    if (!checkPayPassword(newPassword, confirmNewPassword)) {
        return;
    }

    $.ajax({
        type: "POST",
        url: "/UserHome/ResetPayPwdbyEmail",
        data: { pwd: newPassword, confirmPwd: confirmNewPassword },
        cache: false,
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data.error == "0") {
                    alert("您已成功修改支付密码.", 1);
                    $("#checkCode").val("");
                    $("#newPassword").val("");
                    $("#confirmNewPassword").val("");
                } else if (data.error == 1) {
                    window.location.href = "/login.html";
                } else {
                    alert("错误信息：" + data.message);
                    $("#checkCode").val("");
                    $("#newPassword").val("");
                    $("#confirmNewPassword").val("");
                }
            }
            lazyLoad.Run();
        }
    });
}

function showBindPhonePop() {
    ShowBack();
    $(".my_pay_pop").show();
}

function closeBindPhonePop() {
    HideBack();
    $(".my_pay_pop").hide();
    payPassword(1);
}

function goBindPhone() {
    window.location.href = "/UserHome/BindPhone";
}

function showCheckIsSetPayPasswordPop() {
    ShowBack();
    $("#checkIsSetPayPasswordPop").show();
}

function hideCheckIsSetPayPasswordPop() {
    $("#checkIsSetPayPasswordPop").hide();
    HideBack();
}

function cancelCheckIsSetPayPasswordPop() {
    $("#checkIsSetPayPasswordPop").hide();
    HideBack();

    $.ajax({
        type: "POST",
        url: "/UserHome/CancelPayPasswordTip",
        data: {  },
        cache: false,
        success: function (data) {
            if (AjaxValidate(data)) {
                if (data.error == 1) {
                    window.location.href = "/login.html";
                }
            } else {
                alert(data);
            }
            lazyLoad.Run();
        }
    });
}

function showInputPayPasswordPop() {
    ShowBack();
    $("#inputPayPasswordPop").show();
    $("#showPwd").show();
    $("#payPassword").hide();
}

function hideInputPayPasswordPop() {
    $("#inputPayPasswordPop").hide();
    HideBack();
}

$(function () {
    $("#showPwd").focus(function () {
        var text_value = $(this).val();
        if (text_value == this.defaultValue) {
            $("#showPwd").hide();
            $("#payPassword").show().focus();
        }
    });
    $("#payPassword").blur(function () {
        var text_value = $(this).val();
        if (text_value == "") {
            $("#showPwd").show();
            $("#payPassword").hide();
        }
    });

})